package com.sc.citylist.util;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

/**
 * @author shuchang
 * @Date 2018/3/16 下午4:29
 */
public class XmlBuilder {


    /**
     * xml转pojo
     * @param c
     * @param xml
     * @return
     */
    public static Object xmlStr2Obj(Class<?> c,String xml) throws JAXBException, IOException {
        Object xmlObject=null;

        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Reader reader = new StringReader(xml);
        xmlObject = unmarshaller.unmarshal(reader);
        if(reader!=null){
            reader.close();
        }
        return xmlObject;
    }
}
